package algorithm.leetcode.I401to600;

public class Q495 {

    public int findPoisonedDuration(int[] timeSeries, int duration) {
        if (timeSeries.length == 0) return 0;

        int res = duration, bound = timeSeries[0] + duration - 1;
        for (int i = 1; i < timeSeries.length; i++) {
            // 下次中毒起始时间与上次结束时间无重叠
            if (timeSeries[i] > bound) res += duration;
            // 下次中毒起始时间与上次结束时间有重叠
            else res += timeSeries[i] + duration - 1 - bound;
            bound = timeSeries[i] + duration - 1;
        }
        return res;
    }

    public static void main(String[] args) {
        Q495 q495 = new Q495();
        System.out.println(q495.findPoisonedDuration(new int[]{1, 3, 7, 11, 40}, 20));
    }
}
